LiveView automatically synchronizes state between the client and server, ensuring a smooth user experience even with network latency.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phoenixframework/phoenix_live_view/llms.txt
Use this file to discover all available pages before exploring further.
The Challenge
In any web application, client and server state can diverge temporarily due to network latency. Consider this scenario:Example: Form Submission Race Condition
Step 1: User types"hello@example." and debounce triggers:
LiveView’s Solution
LiveView automatically handles this synchronization:- Client is source of truth for current input values
- Tracks in-flight events and only applies changes once all events resolve
- Preserves focus state across server updates
Optimistic UI with Loading Classes
LiveView automatically adds CSS loading classes to elements pushing events to the server.Automatic Loading Classes
- On click: receives
phx-click-loadingclass - On keydown: receives
phx-keydown-loadingclass - Classes removed when server acknowledges the event
Available Loading Classes
| Event | Loading Class |
|---|---|
phx-click | phx-click-loading |
phx-change | phx-change-loading |
phx-submit | phx-submit-loading |
phx-focus | phx-focus-loading |
phx-blur | phx-blur-loading |
phx-window-keydown | phx-keydown-loading |
phx-window-keyup | phx-keyup-loading |
CSS Loading States
Create immediate visual feedback with CSS:Form-Specific Behavior
For events inside forms, loading classes apply to both the element and the form:- Input change:
phx-change-loadingon both input and form - Form submit:
phx-submit-loadingon both button and form
Disabled State
Customize button text during loading:Buttons are automatically disabled during server acknowledgement. Use
phx-disable-with to customize the text.Tailwind Integration
Add custom variants for cleaner loading states:Optimistic UI with JS Commands
For more control, use JS commands to specify which elements get loading states:Common Patterns
- Immediate Hide
- Fade Out
- Add Class
- Multiple Effects
Hide element immediately on click:
JS commands are DOM-patch aware—operations persist across server updates.
Custom Client Events
Dispatch custom DOM events for specialized interactions:Advanced: JS Hooks
For complex cases, use hooks to control exactly when server updates apply:Live Navigation
LiveView provides classes and events for navigation state:Connection Classes
Applied to the LiveView’s parent container:| Class | Description |
|---|---|
phx-connected | View connected to server |
phx-loading | View not connected to server |
phx-error | Error occurred (applied with phx-loading) |
Page Loading Events
Triggered for navigation via<.link>, push_navigate, push_patch, and phx-submit:
Navigation Event
Triggered whenever the URL changes:Complete Example: Optimistic Delete
Best Practices
- Use Loading Classes
- JS Commands for Complex UI
- Hooks for Full Control
- Test with Latency
Start with CSS loading classes for simple feedback:
See Also
- JS Commands - Client-side operations
- Form Bindings - Form handling details
- Hooks - Client-side lifecycle hooks
- Bindings - All available bindings